package com.hxx.day4.controller;

import com.hxx.day4.domain.User;
import com.hxx.day4.service.UserService;
import com.hxx.day4.service.impl.UserServiceImpl;

import java.util.Scanner;

public class UserController {
    // 业务逻辑层对象
    private UserService userService = new UserServiceImpl();

    public void save(Scanner scanner){//接收控制台的输入
        User user = new User();
        System.out.println("请输入id");
        user.setId(scanner.nextInt());
        System.out.println("请输入姓名");
        user.setName(scanner.next());
        System.out.println("请输入性别");
        user.setSex(scanner.next());
        System.out.println("请输入年龄");
        user.setAge(scanner.nextInt());
        //调用业务逻辑层方法
        userService.save(user);
    }
    public void update(Scanner scanner){ //更新
        User user = new User();
        System.out.println("请输入需要更新的id值");
        user.setId(scanner.nextInt());
        System.out.println("请输入姓名");
        user.setName(scanner.next());
        System.out.println("请输入性别");
        user.setSex(scanner.next());
        System.out.println("请输入年龄");
        user.setAge(scanner.nextInt());
        //调用业务逻辑层方法
        userService.update(user);
    }
    public void delete(Scanner scanner){ //删除
        System.out.println("请输入需要删除的id值");
        int id = scanner.nextInt();
        //调用业务逻辑层方法
        userService.delete(id);
    }

    public void show(){
        User[] users = userService.findAll();
        for (User user : users) {
            if (user != null){
                System.out.println(user);
            }
        }
    }

}
























